package mybatis.web;

import mybatis.pojo.Grade;
import mybatis.pojo.Subject;
import mybatis.services.GradeService;
import mybatis.services.SubjectService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author 汤舒青
 * @Version 1.0
 */

@WebServlet("/subjectUpdate")
public class SubjectUpdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        // PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        long suBid = Long.valueOf(request.getParameter("suBid"));
        Subject subject = SubjectService.ME.findsubject(suBid);
        List<Grade> all = GradeService.ME.findAll();
        request.setAttribute("all",all);
        request.setAttribute("subject", subject);
        request.getRequestDispatcher("/WEB-INF/jsp/subjectUpdate.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String subName = request.getParameter("subName");
        int classHour = Integer.parseInt(request.getParameter("classHour"));
        Long gradeId = Long.parseLong(request.getParameter("gradeId"));
        Long id =Long.parseLong( request.getParameter("id"));
        Subject subject = new Subject();
        subject.setId(id);
        subject.setSubjectname(subName);
        subject.setClasshour(classHour);
        subject.setGradeid(gradeId);
        int update = SubjectService.ME.update(subject);
        if (update > 0) {
            out.println("<script>alert('修改成功');window.location.replace('subject');</script>");
        } else {
            out.println("<script>alert('修改失败');window.location.replace('subject');</script>");
        }
    }
}
